﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Drawing;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Security.Cryptography;
using System.IO.Compression;

using VirtualShare;
using VirtualShare.Common;

namespace VirtualShare.Cliendt
{
    public class Client : Common.Client
    {
        enum State
        {
            Idle,
            Walking,
            Talking,
        }
        enum Orientation
        {
            Front,
            Left,
            Right,
        }

        State state = State.Idle;
        Orientation orientation = Orientation.Front;
        int position = Screen.PrimaryScreen.WorkingArea.Width / 2;

        public PictureBox control;
        public Form form;
        public ToolTip tooltip;

        private Connection _msgConnection = null;
        public Connection msgConnection
        {
            get
            {
                return _msgConnection;
            }
            set
            {
                _msgConnection = value;
                _msgConnection.Stream.BeginRead(_msgConnection.buffer, 0, 2, new AsyncCallback(ReceiveMessage), null);
            }
        }

        public Client(BinaryReader reader)
        {
            id = reader.ReadInt32();
            name = reader.ReadString();
            ip = new IPAddress(reader.ReadBytes(4));

            form = new Form();
            form.FormBorderStyle = FormBorderStyle.None;
            form.BackColor = Color.Fuchsia;
            form.TransparencyKey = Color.Fuchsia;
            form.BackgroundImageLayout = ImageLayout.None;
            form.BackgroundImage = Bitmap.FromFile("data/d1.png");
            form.Size = form.BackgroundImage.Size;
            form.StartPosition = FormStartPosition.Manual;
            form.TopMost = true;
            form.Show();
            form.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form.BackgroundImage.Height;
            form.Left = new Random().Next(Screen.PrimaryScreen.Bounds.Width - form.BackgroundImage.Width);

            ToolTip tooltip = new ToolTip();
            tooltip.InitialDelay = 1;
            tooltip.SetToolTip(form, name);
        }

        public void SendMessage(string msg)
        {
            if (msgConnection == null)
            {
                msgConnection = new Connection(new TcpClient(new IPEndPoint(ip, 2424)));
                msgConnection.Writer.Write((short)PacketType.MessageConnection);
                msgConnection.Writer.Write(id);
            }
            msgConnection.Writer.Write((short)PacketType.Message);
            msgConnection.Writer.Write(msg);
        }

        public void ReceiveMessage(IAsyncResult asyncResult)
        {
            msgConnection.Stream.EndRead(asyncResult);
            if ((PacketType)BitConverter.ToInt16(msgConnection.buffer, 0) != PacketType.Message)
                return;
            MessageBox.Show(msgConnection.Reader.ReadString());
        }
    }
}
